package com.fyales.android.juhejoke.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fyales.android.api.model.Joke;
import com.fyales.android.juhejoke.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author fyales
 * @since 16/1/29.
 */
public class TextJokeAdapter extends BaseAdapter {

    private Context context;
    private List<Joke> list;

    public TextJokeAdapter(Context context, List<Joke> jokeList) {
        this.context = context;
        this.list = jokeList;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Joke getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_joke_text, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else{
            holder = (ViewHolder) convertView.getTag();
        }

        final Joke joke = getItem(position);
        holder.contentTv.setText(joke.getContent().trim());
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_joke_text.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.content_tv)
        TextView contentTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
